﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

// might need to rename this class in the future
namespace MechaShark_windows_
{
    class Camera
    {
        //-------------Members-------------
        public Matrix currentView;
        public Matrix currentProj;
        public Matrix currentWorld;
        public float cameraArc = 0;
        public float cameraRotation = 0;
        public float cameraDistance = 70;
        public int Health = 1;

        public float xpos = 30;
        public float ypos = 0;
        public float speed = 0.05f;

        //-------------Methods-------------

        public Camera()
        {

        }

        // update Camera
        #region camera update
        public bool Update(GameTime gameTime,  KeyboardState currentKeyboardState)
        {
            bool isMoving = false;
            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            // Check for input to rotate the camera up and down around the model.

            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                cameraArc += time * speed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                cameraArc -= time * 0.1f;
            }

            //Rotation computation just in case we need it in the future
            //cameraRotation += currentGamePadState.ThumbSticks.Right.X * time * speed;

            // Limit the arc movement. ** now unlimited **
            /*
            if (cameraArc > 90.0f)
                cameraArc = 90.0f;
            else if (cameraArc < -90.0f)
                cameraArc = -90.0f;
            */

            if (currentKeyboardState.IsKeyDown(Keys.W))
            {
                isMoving = true;
                if (25 <= ypos)
                {
                    ypos = 25;
                }
                else
                {
                    ypos += time * speed;
                }
            }
            if (currentKeyboardState.IsKeyDown(Keys.S))
            {
                isMoving = true;
                if (ypos <= -20)
                {
                    ypos = -20;
                }
                else
                {
                    ypos -= time * speed;
                }
            }
            if (currentKeyboardState.IsKeyDown(Keys.A))
            {
                isMoving = true;
                if (xpos <= -40)
                {
                    xpos = -40;
                }
                else
                {
                    xpos -= time * speed;
                }
            }
            if (currentKeyboardState.IsKeyDown(Keys.D))
            {
                isMoving = true;
                if (xpos >= 40)
                {
                    xpos = 40;
                }
                else
                {
                    xpos += time * speed;
                }
            }

            return isMoving;
        }
        #endregion

        // Compute camera matrices.
        public void ComputeMatricesToDraw(GraphicsDevice Device)
        {
            currentWorld =
                Matrix.CreateRotationY(MathHelper.ToRadians(270)) * //90 degrees to cameraRotation if we want rotation
                Matrix.CreateRotationX(MathHelper.ToRadians(cameraArc)) *
                Matrix.CreateTranslation(xpos, ypos, 0);

            currentView = Matrix.CreateRotationY(MathHelper.ToRadians(0)) * //90 degrees to cameraRotation if we want rotation
            Matrix.CreateRotationX(MathHelper.ToRadians(0)) *
            Matrix.CreateTranslation(0, 0, -cameraDistance);

            currentProj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(50),
                                                                    Device.Viewport.AspectRatio,
                                                                    1,
                                                                    1000);
     
        }
    }
}
